Repository navigation
Conversation
…ling Follow-up to ml-explore#748. A compressed cache converted every layer in one graph, and the runtime kept the full-precision leaves until it returned, so the whole cache existed twice while it converted. A long first prompt also landed in full precision whole, because generation compressed only after the prefill. The estimate described that, but the runtime could do better. Design - Each converted layer is evaluated before the next converts, and applyKVCacheConfigurationValidated and maybeTurboQuantizeKVCache no longer hold the old leaves, so one layer holds both copies at a time. - PrefillParameters.cacheCompression chooses when an affine cache compresses a prompt that crosses its start. .whilePrefilling, the default, stops the prefill right after the start, compresses, and writes the rest of the prompt compressed, like mlx-lm. It reuses the prefill split of prompt checkpoints, in TokenIterator and SpeculativeTokenIterator. .afterPrefill keeps the previous behaviour. Prompts with media, non-balanced chunking, and the MTP and block diffusion iterators compress after the prefill. - GenerationEstimator follows both: peakBytes converts layers in order, and Cache.prefillCompression says when a prompt compresses. - Layer.checkpoint and checkpointBytes(tokens:) estimate what a ChatSession prompt checkpoint copies: sliding windows, recurrent state, a requested capacity once it evicts, and an incomplete variance-normalized tile. An app sizing a session against memory missed them before. Measured on an M5 Max, release build: - Converting a 32k cache on Qwen3-Reranker-0.6B-4bit, peak: affine 5.16 -> 4.39 GB, TurboQuant 5.51 -> 4.42 GB. Variance normalization is unchanged at 5.87 GB: its peak is the float32 working memory of one layer, which the estimate still leaves out. - 32k affine prefill on the same model: peak 4.2 -> 2.4 GB above the weights, same speed. Costs and limits - On head dim 256 (Qwen3.8-27B-4bit, 8k prompt) the prompt past the start attends through quantized matmuls: +190 MB peak and 10% slower prefill. .afterPrefill turns it off. - TurboQuant and variance normalization always compress after the prefill. Compressing them while prefilling was 13-18x slower and peaked higher. Tests check that the split matches two generations that end at the start, that both speculative caches compress, that a layer is freed before the next converts, and that checkpointBytes matches real checkpoints for sliding windows, recurrent state, a requested capacity and variance normalization. AI usage: Filo, an AI coding assistant, wrote the code, tests and this message, and ran the unit tests, the DocC check and the measurements.
aleroot
force-pushed
the
alessio/estimator-followups
branch
from
December 2, 2026 18:01
8c2ad04 to
0dcf8cb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Follow-up to #748, for @davidkoski's comment on the compression start and prefill.
A compressed cache converted every layer in one graph, and the runtime kept the full-precision leaves until it returned, so the whole cache existed twice while it converted. A long first prompt also landed in full precision whole, because generation compressed only after the prefill.
PrefillParameters.cacheCompression. With.whilePrefilling, the default, an affine cache stops the prefill right after its compression start, compresses, and writes the rest of the prompt compressed, like mlx-lm..afterPrefillkeeps the previous behaviour. Prompts with media and the MTP and block diffusion iterators still compress after the prefill.peakBytesfollows both. NewcheckpointBytes(tokens:)reports what aChatSessionprompt checkpoint copies (sliding windows, recurrent state, a requested capacity once it evicts), which an app sizing a session against memory missed before.Measured on an M5 Max, Qwen3-Reranker-0.6B-4bit, 32k tokens:
Costs and limits:
.afterPrefillturns it off.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.